<template>
    <div class="com-container">
      <div class="title" :style="comStyle">
        <span>{{ '🌴' + showTitle}}</span>
        <span class="iconfont title-icon" :style="comStyle" @click="showChoice = !showChoice">&#xe6eb;</span>
        <div v-show="showChoice">
          <div class="select-item" v-for="item in selectTypes" :key="item.key" @click="handleSelect(item)" :style="marginStyle">{{item.text}}</div>
        </div>
      </div>
      <div class="com-chart" ref="trend_ref"></div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_utils'
export default {
  data () {
    return {
      chartInstane: null,
      allData: null,
      showChoice: false,
      choiceType: 'map',
      titleFontSize: 0 // 标题字体大小
    }
  },
  computed: {
    selectTypes () {
      if (!this.allData) {
        return []
      } else {
        return this.allData.type.filter(item => item.key !== this.choiceType)
      }
    },
    showTitle () {
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.choiceType].title
      }
    },
    // 设置标题样式
    comStyle () {
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor
      }
    },
    // 计算标题缩进
    marginStyle () {
      return {
        marginLeft: this.titleFontSize + 'px'
      }
    },

    ...mapState(['theme'])
  },
  watch: {
    theme () {
      this.chartInstane.dispose()
      this.initChart()
      this.screenAdapter()
      this.getData()
    }
  },
  mounted () {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    // 初始化图表
    initChart () {
      this.chartInstane = this.$echarts.init(this.$refs.trend_ref, this.theme)
      const option = {
        grid: {
          left: '8%',
          top: '35%',
          right: '4%',
          bottom: '5%',
          containLable: true // 包含坐标轴文字
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          left: 20,
          top: '20%',
          icon: 'circle'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false // 是否需要间隔
        },
        yAxis: {
          type: 'value'
        }
      }
      this.chartInstane.setOption(option)
    },

    // 获取数据
    async getData () {
      const { data: result } = await this.$http.get('trend')
      this.allData = result
      this.updateChart()
    },

    // 更新图表数据
    updateChart () {
      // 半透明的颜色值
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)'
      ]
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)'
      ]
      // 获取类目轴数据
      const timeArr = this.allData.common.month
      // 获取y轴数据
      const valueArr = this.allData[this.choiceType].data
      const seriesArr = valueArr.map((item, index) => {
        return {
          type: 'line',
          name: item.name,
          data: item.data,
          stack: this.choiceType,
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: colorArr1[index] }, { offset: 1, color: colorArr2[index] }
            ])
          }
        }
      })
      // 图例数据
      const legendArr = valueArr.map(item => item.name)
      const dataOption = {
        xAxis: {
          data: timeArr
        },
        legend: {
          data: legendArr
        },
        series: seriesArr
      }
      this.chartInstane.setOption(dataOption)
    },

    // 屏幕适配器
    screenAdapter () {
      this.titleFontSize = this.$refs.trend_ref.offsetWidth / 100 * 3.6
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      }
      this.chartInstane.setOption(adapterOption)
      this.chartInstane.resize()
    },

    // 选着类型
    handleSelect (tyep) {
      this.choiceType = tyep.key
      this.updateChart()
      this.showChoice = false
    }
  }
}
</script>

<style lang='scss' scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-con {
    background-color: #333333;
  }
}
</style>
